package cn.tedu.controller;

import cn.tedu.controller.BaseAndEx.BaseController;
import cn.tedu.entity.User;
import cn.tedu.service.ex.PasswordExcption;
import cn.tedu.service.ex.UserNotFoundException;
import cn.tedu.service.port.UserService;
import cn.tedu.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    //自动处理自定异常类UserNotFound的异常
    @ResponseBody
    @ExceptionHandler(UserNotFoundException.class)
    public JsonResult HandlerUserNotFound(Exception e){
        e.printStackTrace();
        return new JsonResult(3, e);
    }

    //自动处理自定异常类Password的异常
    @ResponseBody
    @ExceptionHandler(PasswordExcption.class)
    public JsonResult HandlerPassword(Exception e){
        e.printStackTrace();
        return new JsonResult(2, e);
    }

    /**
     * 检查用户名是否重复
     * @param username 用户名信息
     * @return         返回用户名
     */
    @ResponseBody
    @RequestMapping("/check_username.do")
    public JsonResult checkUsername(String username) {
        Object data = userService.registUser(username);
        return new JsonResult(data);
    }

    /**
     * 检查邮箱地址是否重复
     * @param email 邮箱信息
     * @return  返回Json对象采用默认构造器
     */
    @ResponseBody
    @RequestMapping("/check_email.do")
    public JsonResult checkEmail(String email) {
        userService.registEmail(email);
        return new JsonResult();
    }

    /**
     * 检查电话号码是否重复
     * @param phone 电话信息
     * @return  返回Json对象采用默认构造器
     */
    @ResponseBody
    @RequestMapping("/check_phone.do")
    public JsonResult checkPhone(String phone) {
        userService.registPhone(phone);
        return new JsonResult();
    }

    /**
     * 用户注册
     * @param username  用户名
     * @param password  密码
     * @param upwdconfirm   确认密码
     * @param email 邮箱地址
     * @param phone 电话号码
     * @return  返回Json对象采用默认构造器
     */
    @ResponseBody
    @RequestMapping(value = "/handle_regist.do", method = RequestMethod.POST)
    public JsonResult handleRegist (@RequestParam("uname") String username,
                                    @RequestParam("upwd") String password,
                                    String upwdconfirm,
                                    String email,
                                    String phone, HttpSession session) {
        userService.regist(username, password, upwdconfirm, email, phone, session);
        return new JsonResult();
    }

    /**
     * 修改密码
     * @param oldpassword   旧密码
     * @param newpassword   新密码
     * @param upwdconfirm   确认新密码
     * @param session   用于获取id
     * @return  返回Json对象采用默认构造器
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "handle_change_password.do")
    public JsonResult handleChangePassword(String oldpassword, String newpassword, String upwdconfirm, HttpSession session) {
        Integer id = getUidSession(session);
        userService.changePassword(id, oldpassword, newpassword, upwdconfirm);
        return new JsonResult();
    }

    /**
     * 用户登录
     * @param username  用户名
     * @param password  密码
     * @return  返回Json对象采用默认构造器
     */
    @ResponseBody
    @RequestMapping(value = "/handlelogin.do", method = RequestMethod.POST)
    public JsonResult handlelgoin(String username, String password, HttpSession session){
        userService.login(username, password, session);
        return new JsonResult();
    }

    /**
     * 修改个人信息
     * @param username  用户名
     * @param gender    性别
     * @param phone 电话
     * @param email 邮箱
     * @return  返回Json对象采用默认构造器
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/handle_profile.do")
    public JsonResult profile(String username, Integer gender, String phone, String email, HttpSession session){
        Integer id = getUidSession(session);
        Integer a = userService.editProfile(id, username, gender, phone, email);
        return new JsonResult();
    }

/*跳转请求分割线--------------------------------------------------------------------------*/

    /**
     * 退出登录 跳转到主页
     * @param session  HttpSession对象
     * @return  返回主页
     */
    @RequestMapping("/logout.do")
    public String logoust(HttpSession session) {
        //清除session信息
        session.invalidate();
        return "index";
    }

    /**
     * 跳转到登录页面
     */
    @RequestMapping("/login.do")
    public String showLogin(HttpServletResponse res){
        return "login";
    }

    /**
     * 跳转到注册页面
     */
    @RequestMapping("/register.do")
    public String showRegister(){
        return "register";
    }

    /**
     * 跳转到修改密码页面
     */
    @RequestMapping("/password.do")
    public String showPassword(){
        return "personal_password";
    }

    /**
     * 跳转到个人信息修改页面
     */
    @RequestMapping("/profile.do")
    public String showProfile(HttpSession session, ModelMap map){
        Integer id = getUidSession(session);
        User user = userService.findUserById(id);
//        System.out.println(user.getPhone());
//        System.out.println(user.getEmail());
        map.addAttribute("user", user);
        return "personage";
    }

//
//    /**
//     * 添加处理图片显示,执行后字节在网页显示该图片
//     * @return
//     * @throws IOException
//     */
//    //@ResponseBody 注解与返回值byte[]配合，填充Response的Body部分
//    @ResponseBody
//    //produces 用于设置content-type 属性
//    @RequestMapping(value = "/demo.do", produces = "image/png")
//    public byte[] pngDemo() throws IOException {
//        String path = "cn/tedu/controller/使用Spring MVC 模式 下载图片 .png";
//        InputStream is = getClass().getClassLoader().getResourceAsStream(path);
//        //available方法可以检测流中一次可以读取
//        //的字节个数，小文件就是文件的长度
//        byte[] bytes = new byte[is.available()];
//        is.read(bytes);
//        return bytes;
//    }
}
